package com.zqweb.gulimall.order.listener;

import com.rabbitmq.client.Channel;
import com.zqweb.common.to.SeckillOrderTo;
import com.zqweb.gulimall.order.service.OrderService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@RabbitListener(queues = {"order.seckill.order.queue"})
@Component
public class OrderSeckillListener {
    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void orderSeckillListen(SeckillOrderTo seckillOrderTo, Channel channel, Message message) throws IOException {
        // 构建秒杀单
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            if(seckillOrderTo != null){
                orderService.buildSeckillOrder(seckillOrderTo);
            }
            // 手动进行消息的ack
            channel.basicAck(deliveryTag,false);// 不进行批量ack
        } catch (Exception e) {
            channel.basicReject(deliveryTag,true);
        }


    }

}
